package com.liao.badminton.code;

import cn.hutool.core.util.StrUtil;

public class ProjectException extends RuntimeException {

    private Integer code;
    private String message;

    public ProjectException(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static void newException(RespCodeMessage resEnum) {
        newException(resEnum, null);
    }

    public static void newException(RespCodeMessage resEnum, String msg) {
        if (StrUtil.isBlank(msg) && resEnum.getMessage().contains("：{}")) {
            throw new ProjectException(resEnum.getCode(), resEnum.getMessage().replace("：{}", ""));
        } else
            throw new ProjectException(resEnum.getCode(), StrUtil.format(resEnum.getMessage(), msg));
    }

    public static void newException(String msg) {
        newException(RespCodeMessage.PARAM_LACK, msg);
    }

    public static void newDataNullException(String msg) throws ProjectException {
        newException(RespCodeMessage.DB_DATA_NULL, msg);
    }

    public static void newDataAlreadyException(String msg) throws ProjectException {
        newException(RespCodeMessage.DB_DATA_EXIST, msg);
    }

}
